package com.lagou.controller;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCConnPool {
    public static DruidPooledConnection druidPooledConnection;
    public static DruidDataSource druidDataSource = new DruidDataSource();


    public static Connection getConnection(String url, String driver, String username, String password, int initialSize, int maxActive, int maxWait) throws SQLException {
        System.out.println("=======================传入的数据是" + url);
        druidDataSource.setUrl(url);
        druidDataSource.setDriverClassName(driver);
        druidDataSource.setUsername(username);
        druidDataSource.setPassword(password);
        druidDataSource.setInitialSize(initialSize);
        druidDataSource.setMaxActive(maxActive);
        druidDataSource.setMaxWait(maxWait);
        druidPooledConnection = druidDataSource.getConnection();
        return druidPooledConnection.getConnection();
    }

    public static void close(Connection con, Statement st) {
        if (con != null && st != null) {
            try {
                con.close();
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void close(Connection con, Statement st, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        close(con, st);
    }
}
